package cc.vv.lkbasecomponent.base.app;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;

import cc.vv.lkbasecomponent.base.service.ConnectChangeService;

/**
 * 基础组件库Application
 *
 * @author: NPF
 * @date: 2018/3/13.
 */
public class LKBaseApplication extends Application {


    private static Handler mHandler;
    private static LKBaseApplication mApplication;

    /**
     * 网络监听是否注册
     */
    public static boolean isConnectionRegister = false;

    @Override
    public void onCreate() {
        super.onCreate();
        mHandler = new Handler();
        mApplication = this;
    }

    /**
     * 开启网络连接状态监听
     */
    protected void openConnectChangeReceiver() {
        isConnectionRegister = true;
        Intent connectChangeService = new Intent(this, ConnectChangeService.class);
        bindService(connectChangeService, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                isConnectionRegister = true;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                isConnectionRegister = false;
            }
        }, Context.BIND_AUTO_CREATE);
    }


    /**
     * 获取Handler
     *
     * @return Handler
     */
    public static Handler getHandler() {
        return mHandler;
    }

    /**
     * 获取上下文
     *
     * @return
     */
    public static LKBaseApplication getApplication() {
        return mApplication;
    }
}
